﻿using CaseManagerLibrary.Http;
using CaseManagerLibrary.Models;
using PSDLib.Download;
using PSDLib.Models;
using PSDManager.SystemSettings;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PSDManager
{
    public partial class FrmAddTaskForCameraSD : Form
    {
        public List<dl_task_camera_sd_start> TaskStart { get; set; }

        public FrmAddTaskForCameraSD()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterParent;
            GetCamera();
        }

        private void GetCamera()
        {
            var result_room = HttpHelper.Get<HResult<List<rm_room_info>>>(AppSettings.Setting.API.GetFreeRoom);
            var result_server = HttpHelper.Get<HResult<List<dl_server_config>>>(AppSettings.Setting.API.GetDownloadServerConfig);

            if (result_room.success && result_server.success)
            {
                foreach (var room in result_room.data)
                {
                    TreeNode node_room = new TreeNode();
                    node_room.Text = room.room_number;
                    node_room.Name = room.id;
                    node_room.Tag = room;
                    tvwFree.Nodes.Add(node_room);

                    var cameraList = result_server.data.Where(t => t.storage_server_name.StartsWith(room.room_number.Split("-".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)[0])).ToList();

                    foreach (var camera in cameraList)
                    {
                        if (camera.storage_server_type == DownloadServerType.Camera.ToString())
                        {
                            string[] temp = camera.storage_server_name.Split("_".ToCharArray());
                            string roomNum = temp[0];
                            string cameraName = temp[1];

                            TreeNode node_camera = new TreeNode();
                            node_camera.Name = camera.storage_server_name;
                            node_camera.Text = cameraName;
                            node_camera.Tag = camera;
                            node_camera.ImageIndex = 1;
                            node_camera.SelectedImageIndex = 1;
                            node_room.Nodes.Add(node_camera);
                        }
                    }
                }

                tvwFree.ExpandAll();
            }
        }

        private void btnAccept_Click(object sender, EventArgs e)
        {
            TaskStart = new List<dl_task_camera_sd_start>();

            foreach (TreeNode nr in tvwSelected.Nodes)
            {
                var ts = new dl_task_camera_sd_start();
                ts.roomNum = nr.Text;
                ts.startTime = dtpStartDate.Value;
                ts.endTime = dtpEndDate.Value;
                ts.cameraList = new List<string>();

                foreach (TreeNode nc in nr.Nodes)
                {
                    ts.cameraList.Add((nc.Tag as dl_server_config).id);
                }

                TaskStart.Add(ts);
            }

            DialogResult = DialogResult.OK;
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (tvwFree.SelectedNode != null)
            {
                var n = tvwSelected.Nodes.Find(tvwFree.SelectedNode.Name, true);

                if (n.Length == 0)
                {
                    if (tvwFree.SelectedNode.Level == 0)
                    {
                        TreeNode node_room_sel = new TreeNode();
                        node_room_sel.Name = tvwFree.SelectedNode.Name;
                        node_room_sel.Text = tvwFree.SelectedNode.Text;
                        node_room_sel.ImageIndex = 0;
                        node_room_sel.SelectedImageIndex = 0;
                        tvwSelected.Nodes.Add(node_room_sel);

                        foreach (TreeNode nc in tvwFree.SelectedNode.Nodes)
                        {
                            TreeNode node_camera_sel = new TreeNode();
                            node_camera_sel.Name = nc.Name;
                            node_camera_sel.Text = nc.Text;
                            node_camera_sel.Tag = nc.Tag;
                            node_camera_sel.ImageIndex = 1;
                            node_camera_sel.SelectedImageIndex = 1;
                            node_room_sel.Nodes.Add(node_camera_sel);
                        }
                    }
                    else if (tvwFree.SelectedNode.Level == 1)
                    {
                        TreeNode node_room_sel;
                        var nr = tvwSelected.Nodes.Find(tvwFree.SelectedNode.Parent.Name, false);
                        if (nr.Length == 0)
                        {
                            node_room_sel = new TreeNode();
                            node_room_sel.Name = tvwFree.SelectedNode.Parent.Name;
                            node_room_sel.Text = tvwFree.SelectedNode.Parent.Text;
                            node_room_sel.ImageIndex = 0;
                            node_room_sel.SelectedImageIndex = 0;
                            tvwSelected.Nodes.Add(node_room_sel);
                        }
                        else
                        {
                            node_room_sel = nr[0];
                        }

                        TreeNode node_camera_sel = new TreeNode();
                        node_camera_sel.Name = tvwFree.SelectedNode.Name;
                        node_camera_sel.Text = tvwFree.SelectedNode.Text;
                        node_camera_sel.Tag = tvwFree.SelectedNode.Tag;
                        node_camera_sel.ImageIndex = 1;
                        node_camera_sel.SelectedImageIndex = 1;
                        node_room_sel.Nodes.Add(node_camera_sel);
                    }
                }
                else
                {
                    MessageBox.Show("已添加相关摄像机！");
                }

                tvwSelected.ExpandAll();
            }
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            if (tvwSelected.SelectedNode != null)
            {
                tvwSelected.SelectedNode.Remove();
            }
        }
    }

    public class dl_task_camera_sd_start
    {
        public string roomNum { get; set; }
        public string deviceId { get; set; }
        public List<string> cameraList { get; set; }
        public DateTime startTime { get; set; }
        public DateTime endTime { get; set; }
    }
}
